package com.example.demo.modules.customDefine.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.util.R;
import com.example.demo.modules.customDefine.entity.UserType;
import com.example.demo.modules.customDefine.service.UserTypeService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@RestController
@RequestMapping("/userType")
public class UserTypeController {

    @Autowired
    private UserTypeService userTypeService;


    @GetMapping("/one")
    public R one(@RequestParam(value = "id", required = true) String id) {
        if(null == id || "".equals(id)){
            return R.error().put("msg", "缺少参数id");
        }

        return R.ok().put("result", userTypeService.getById(id));
    }
    
    @GetMapping("/listAll")
    public R listAll() {
        return R.ok().put("result",userTypeService.list(null));
    }

    @GetMapping("/page")
    @ApiImplicitParams({
            @ApiImplicitParam(name="pageSize",dataType="String"),
            @ApiImplicitParam(name="pageNum",dataType="String")
    })
    public R page(
            Integer pageSize,Integer pageNum) {
        if (null == pageSize || "".equals(pageSize)) {
            pageSize = 10;
        }
        if (null == pageNum || "".equals(pageNum)) {
            pageNum = 0;
        }
        QueryWrapper<UserType> queryWrapper = new QueryWrapper<>();
        return R.ok().put("result",userTypeService.page(new Page<>(pageNum,pageSize), queryWrapper));
    }
    @PostMapping("/add")
    public R add(@RequestBody UserType userType) {
        return R.ok().put("result",userTypeService.save(userType));
    }
    @PostMapping("/update")
    public R update(@RequestBody  UserType userType, HashMap<String, Object> updateOptions) {
        QueryWrapper<UserType> queryWrapper=new QueryWrapper<>();
        if(null != userType.getId() && !("".equals(userType.getId()))){
            queryWrapper.eq("id",userType.getId());
        }else{
          return R.error("id不能为空");
        }
        return R.ok().put("result",userTypeService.update(userType,queryWrapper));
    }
    
    @GetMapping("/delete")
    public R deleteOne(@RequestParam(value = "id",required = true) String id) {
        return R.ok().put("result", userTypeService.removeById(id));
    }
}
